<?php

namespace App\Admin\Controllers;

use App\Models\AdminMenusModel;
use App\Models\AdminRolesModel;

class RolesController extends Controller
{
    public function list()
    {
        $page_size = $this->request->get('size', 10);
        $page = $this->request->get('page', 1);
        $offset = ($page - 1) * $page_size;
        $offset = $offset > 0 ? $offset : 0;

        $admin_roles = AdminRolesModel::query()->limit($page_size)->offset($offset)->get();
        $total = AdminRolesModel::query()->count();
        if (empty($admin_roles)) {
            return $this->success(['list' => [], 'total' => $total]);
        }

        $admin_menus = AdminMenusModel::query()->get(['id', 'pid'])->toArray();
        $admin_menus = array_column($admin_menus, null, 'id');

        $admin_roles_arr = $admin_roles->toArray();
        foreach ($admin_roles_arr as &$admin_role) {
            $admin_role['menus'] = [];
            $admin_role_menu = array_filter(explode(',', $admin_role['menu_ids']));
            foreach ($admin_role_menu as $role_menu) {
                $arr = [];
                $this->getHasRoles($admin_menus, $role_menu, $arr);
                $admin_role['menus'][] = $arr;
            }
        }
        unset($admin_role);

        return $this->success(['list' => $admin_roles_arr, 'total' => $total]);
    }

    protected function getHasRoles($admin_menus, $id, &$arr)
    {
        if ($admin_menus[$id]['pid'] === 0) {
            $arr[] = $id;
        } else {
            $this->getHasRoles($admin_menus, $admin_menus[$id]['pid'], $arr);
            $arr[] = $id;
        }
        return true;
    }

    public function selectList()
    {
        $admin_roles = AdminRolesModel::query()->get();
        if (empty($admin_roles)) {
            return $this->success();
        }

        return $this->success($admin_roles->toArray());
    }

    public function addOrEditRole()
    {
        $id = $this->request->post('id', 0);
        $name = $this->request->post('name', '');
        $menus = $this->request->post('menus', []);

        $menu_ids = [];
        foreach ($menus as $menu) {
            $menu_ids = array_values(array_unique(array_merge($menu_ids, $menu)));
        }
        asort($menu_ids);

        if ($id) {
            $re = AdminRolesModel::query()->where('id', $id)->update([
                'name' => $name,
                'menu_ids' => implode(',', $menu_ids)
            ]);
            if (!$re) {
                return $this->fail(CODE_ERR_SYSTEM, '更新角色名称失败');
            }

        } else {
            $id = AdminRolesModel::query()->insertGetId([
                'name' => $name,
                'menu_ids' => implode(',', $menu_ids)
            ]);
            if (!$id) {
                return $this->fail(CODE_ERR_SYSTEM, '创建新角色失败');
            }
        }

        return $this->success();
    }

    public function del()
    {
        $id = $this->request->post('id', 0);

        $re = AdminRolesModel::query()->where('id', $id)->delete();

        if (!$re) {
            $this->fail(CODE_ERR_SYSTEM, '删除失败');
        }

        return $this->success();
    }
}
